
package com.gxa.ssm251.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.IOException;

@Component
@Aspect
public class LogAspects {
//    OutputStream outputStream = null;
//    SimpleDateFormat simpleDateFormat = null;
//    {
//        try {
//            File file = new File("D:\\1/log.txt");
//            file.createNewFile();
//           outputStream = new FileOutputStream(file,true);
//            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
//            simpleDateFormat  = new SimpleDateFormat(strDateFormat);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Pointcut(value = "execution(* com.gxa.ssm251.service.impl.*.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) throws FileNotFoundException {
//        1. 执行的方法名+方法的参数
//        2. 记录执行结果
//        3. 记录在绝对路径: d:/log/....log
//        String s = joinPoint.toString();
//        Object[] args = joinPoint.getArgs();
//        StringBuilder stringBuilder = new StringBuilder();
//        for (Object arg : args) {
//            String s1 = arg.toString();
//            stringBuilder = stringBuilder.append(s1);
//        }
//        try{
//            byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
//            String s1 = args.toString();
//            byte[] bytes1 = stringBuilder.toString().getBytes(StandardCharsets.UTF_8);
//
//            String format = simpleDateFormat.format(System.currentTimeMillis());
//            outputStream.write(format.getBytes(StandardCharsets.UTF_8));
//            outputStream.write("\r".getBytes(StandardCharsets.UTF_8));
//            outputStream.write(bytes);
//            outputStream.write("\r\n".getBytes(StandardCharsets.UTF_8));
//            outputStream.write(bytes1);
//            outputStream.write("\r\n".getBytes(StandardCharsets.UTF_8));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println(s);
        System.out.println("前置通知");
    }

    @After("pointcut()")
    public void after(){
        System.out.println("后置通知");
    }

    @AfterReturning(value ="pointcut()",returning = "result")
    public void afterReturning(Object result) throws IOException {
//        outputStream.write(result.toString().getBytes(StandardCharsets.UTF_8));
//        outputStream.write("\r\n".getBytes(StandardCharsets.UTF_8));
        System.out.println("后置返回通知");
    }

    @AfterThrowing(value = "pointcut()")

    public void afterThrowing(){
        System.out.println("后置抛出通知");
    }

//    @Around("pointcut()")
//    public void around(){
//
//    }

}
